﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ContaTelefone.Process;
using ContaTelefone.Entities;

namespace ContaTelefone
{
    public partial class frmCCUS : Form
    {
        CCustoProcess ccP = new CCustoProcess();

        public frmCCUS()
        {
            InitializeComponent();

            dgvCCUS.DataSource = ccP.Listar();

        }

        private void tsbNovo_Click(object sender, EventArgs e)
        {
            frmCCUSEdit formedit = new frmCCUSEdit(null);
            formedit.ShowDialog();
            dgvCCUS.DataSource = ccP.Listar();
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvCCUS.SelectedRows.Count >= 1)
            {
                CCustoEntities ccEtt = (CCustoEntities)dgvCCUS.SelectedRows[0].DataBoundItem;
                frmCCUSEdit formedit = new frmCCUSEdit(ccEtt);
                formedit.ShowDialog();
                dgvCCUS.DataSource = ccP.Listar();
            }
        }

        private void tsbFechar_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvCCUS.SelectedRows.Count >= 1)
                {
                    if (MessageBox.Show("Tem certeza que deseja excluir o registro selecionado?", "Conta Telefone", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        CCustoEntities ccEtt = (CCustoEntities)dgvCCUS.SelectedRows[0].DataBoundItem;
                        ccP.Delete(ccEtt);
                        dgvCCUS.DataSource = ccP.Listar();
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    MessageBox.Show(ex.Message + "\r\n" + ex.InnerException.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    
}
